package com.probiz.estore.catalog.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.catalog.dao.GiftCategoryDao;
import com.probiz.estore.catalog.model.GiftCategorySearchCriteria;
import com.probiz.estore.common.model.catalog.GiftCategory;
import com.probiz.estore.core.search.SearchCriteria;

/**
 * Dao implementation for GiftCategory.
*/
public class GiftCategoryDaoImpl extends BaseCategoryDaoImpl<GiftCategory> implements GiftCategoryDao  {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.dao.GiftCategoryDao#getCategoryByCode(java.lang.String)
	 */
	public GiftCategory getCategoryByCode(String categoryCode) {
		return (GiftCategory) this.findUnique(
				"from GiftCategory c where c.categoryCode=?", categoryCode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.dao.GiftCategoryDao#listCategoryByParentIdAndName(java.lang.Integer,
	 *      java.lang.String)
	 */
	public List<GiftCategory> listCategoryByParentIdAndName(
			Integer parentCategoryId, String categoryName) {
		String hql = "from GiftCategory c where c.parent.categoryId=? and c.categoryName=?  order by c.sortOrder, c.categoryId";
		List<GiftCategory> categoryList = findByHql(hql, new Object[] {
				parentCategoryId, categoryName });
		return categoryList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.dao.GiftCategoryDao#searchByCriteria(com.probiz.estore.catalog.model.GiftCategorySearchCriteria,
	 *      com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<GiftCategory> searchByCriteria(
			GiftCategorySearchCriteria pcSearchCriteria,
			SearchCriteria searchCriteria) {
		StringBuffer hqlsb = new StringBuffer(100);
		List<Object> paramList = new ArrayList<Object>();
		// start:构建HQL语句
		hqlsb.append("from GiftCategory c where 1=1 ");

		if (StringUtils.isNotBlank(pcSearchCriteria.getCategoryName())) {
			hqlsb.append(" and pc.categoryName like ?");
			paramList.add("%" + pcSearchCriteria.getCategoryName() + "%");
		}

		if (null != pcSearchCriteria.getStatus()) {
			hqlsb.append(" and pc.status = ?");
			paramList.add(pcSearchCriteria.getStatus());
		}
		// end:构建HQL语句
		searchCriteria.setHql(hqlsb.toString());
		searchCriteria.removeParamValues();
		for (Object object : paramList) {

			searchCriteria.addParamValue(object);
		}
		return searchByCriteria(searchCriteria);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.dao.GiftCategoryDao#getNavigationCategories(java.lang.Integer)
	 */
	public List<GiftCategory> getNavigationCategories(Integer categoryId) {
		List<GiftCategory> navigationCategories = new ArrayList<GiftCategory>();
		GiftCategory giftCategory = this.getById(categoryId);
		if (giftCategory == null)
			return navigationCategories;
		String categoryPath = giftCategory.getCategoryPath();
		if (StringUtils.isNotEmpty(categoryPath)) {
			String categoryIds[] = categoryPath.split("\\.");
			for (int i = 1; i < categoryIds.length; i++) {
				GiftCategory parentCategory = this.getById(Integer
						.parseInt(categoryIds[i]));
				navigationCategories.add(parentCategory);
			}
		}
		return navigationCategories;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.dao.GiftCategoryDao#getSizeByCategoryCodeByNotCategoryId(java.lang.String,
	 *      java.lang.Integer)
	 */
	public Long getSizeByCategoryCodeByNotCategoryId(final String categoryCode,
			final Integer categoryId) {
		Long size;
		if (categoryId != null) {

			size = (Long) getHibernateTemplate().executeWithNewSession(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							StringBuffer sb = new StringBuffer();
							sb
									.append("select count(*) from GiftCategory c where c.categoryCode = ? and c.categoryId != ?");
							Query query = session.createQuery(sb.toString());
							query.setString(0, categoryCode);
							query.setInteger(1, categoryId);
							Long rows = (Long) query.uniqueResult();
							return rows;
						}

					});
		} else {
			size = (Long) getHibernateTemplate().executeWithNewSession(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							StringBuffer sb = new StringBuffer();
							sb
									.append("select count(*) from GiftCategory c where c.categoryCode = ?");
							Query query = session.createQuery(sb.toString());
							query.setString(0, categoryCode);
							Long rows = (Long) query.uniqueResult();
							return rows;
						}

					});
		}
		return size;
	}

	/*
	 * 
	 */
	public List<String> getAllgiftCategoryCodes() {
		List<String> categoryCodeList = findByHql("select c.categoryCode from GiftCategory c where c.categoryId!=1  c.status!=-1");
		return categoryCodeList;
	}
	
	public List<GiftCategory> getCategorysByCategoryPath(String categoryPath){
		String hql = " from GiftCategory g where g.categoryPath like ? order by g.categoryPath desc";
		List<GiftCategory> giftCategoryList = this.findByHql(hql, new Object[]{categoryPath + "%"});
		return giftCategoryList;
	}
}
